"ui";
// importClass(android.content.BroadcastReceiver);
importClass(android.content.IntentFilter);

ui.layout(
    <vertical>
        <card padding="10" w="*" h="auto" margin="5 5 5 5" cardCornerRadius="20dp" cardBackgroundColor="#72ffffff" cardElevation="1dp" alpha="0.8" cardCornerRadius="30dp">
            <horizontal margin="20 10 20 5" gravity="center" >
                <button id="open" text="打开广播" layout_weight="1" style="Widget.AppCompat.Button.Colored" />;
                <button id="cq" text="重启" layout_weight="1" style="Widget.AppCompat.Button.Colored" />;
                <button id="close" text="关闭广播" layout_weight="1" style="Widget.AppCompat.Button.Colored" />;
            </horizontal>
        </card>

        <card padding="10" w="*" h="auto" margin="5 5 5 5" cardCornerRadius="20dp" cardBackgroundColor="#72ffffff" cardElevation="1dp" alpha="0.8" cardCornerRadius="30dp">
            <horizontal margin="20 10 20 5" gravity="center" >
                <text text="选择电平:" textColor="black" w="auto" />
                <spinner id="选择电平7" textStyle="bold" entries="高电平|低电平" spinnerMode="dialog" />
                <button id="设置GPIO7" text="设置GPIO7" layout_weight="1" style="Widget.AppCompat.Button.Colored" />;
            </horizontal>
        </card>

        <card padding="10" w="*" h="auto" margin="5 5 5 5" cardCornerRadius="20dp" cardBackgroundColor="#72ffffff" cardElevation="1dp" alpha="0.8" cardCornerRadius="30dp">
            <horizontal margin="20 10 20 5" gravity="center" >
                <text text="选择电平:" textColor="black" w="auto" />
                <spinner id="选择电平8" textStyle="bold" entries="高电平|低电平" spinnerMode="dialog" />
                <button id="设置GPIO8" text="设置GPIO8" layout_weight="1" style="Widget.AppCompat.Button.Colored" />;
            </horizontal>
        </card>
        <card padding="10" w="*" h="auto" margin="5 5 5 5" cardCornerRadius="20dp" cardBackgroundColor="#72ffffff" cardElevation="1dp" alpha="0.8" cardCornerRadius="30dp">
            <horizontal margin="20 10 20 5" gravity="center" >
                <text text="选择电平:" textColor="black" w="auto" />
                <spinner id="选择电平9" textStyle="bold" entries="高电平|低电平" spinnerMode="dialog" />
                <button id="设置GPIO9" text="设置GPIO9" layout_weight="1" style="Widget.AppCompat.Button.Colored" />;
            </horizontal>
        </card>
    </vertical>
);

let intent = new Intent();
let filter = new IntentFilter();
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
filter.addAction("android.intent.action.Dustin");
filter.addAction("android.intent.action.激活路飞");
filter.addAction("com.fise.system.reboot");
filter.addAction("echo 1 > /sys/class/fise_gpio5/level");
filter.addAction("echo 0 > /sys/class/fise_gpio5/level");
filter.addAction("echo 1 > /sys/class/fise_gpio6/level");
filter.addAction("echo 0 > /sys/class/fise_gpio6/level");
filter.addAction("echo 1 > /sys/class/fise_gpio7/level");
filter.addAction("echo 0 > /sys/class/fise_gpio7/level");
filter.addAction(Intent.ACTION_SCREEN_ON);

let flag = false;
let receiver = new JavaAdapter(android.content.BroadcastReceiver, {
    onReceive: function (context, intent) {
        log(intent);
        switch (intent.action) {
            case Intent.ACTION_BATTERY_CHANGED:
                log("ACTION_BATTERY_CHANGED");
                let level = intent.getIntExtra("level", 0);
                toastLog("当前电量:" + level + "%");
                break;
            case "android.intent.action.Dustin":
                toastLog("自定义广播");
                let value = intent.getStringExtra("author");
                log("接收到数据 author: " + value);
                break;
            case Intent.ACTION_SCREEN_ON:
                toastLog("亮屏");
                break;
            case "com.fise.system.reboot":
                toastLog("重启");
                break;
            case "echo 1 > /sys/class/fise_gpio7/level":
                toastLog("您选择的是GPIO7高电平！");
                break;
            case "echo 0 > /sys/class/fise_gpio7/level":
                toastLog("您选择的是GPIO7低电平！");
                break;
            case "echo 1 > /sys/class/fise_gpio8/level":
                toastLog("您选择的是GPIO8高电平！");
                break;
            case "echo 0 > /sys/class/fise_gpio8/level":
                toastLog("您选择的是GPIO8低电平！");
                break;
            case "echo 1 > /sys/class/fise_gpio9/level":
                toastLog("您选择的是GPIO9高电平！");
                break;
            case "echo 0 > /sys/class/fise_gpio9/level":
                toastLog("您选择的是GPIO9低电平！");
                break;

        }
    },
});

ui.open.on("click", () => {
    if (flag) {
        return true;
    }
    context.registerReceiver(receiver, filter);
    flag = true;
});

ui.cq.on("click", () => {
    var action = "com.fise.system.reboot";
    app.sendBroadcast({
        action: action,
        extras: {
            author: "Dustin",
        },
    });
});

ui.设置GPIO7.on("click", () => {
    var i = ui.选择电平7.getSelectedItemPosition();
    switch (i) {
        case 0:
            toast("您选择的高电平！");
            var action = "echo 1 > /sys/class/fise_gpio5/level";
            app.sendBroadcast({
                action: action,
                extras: {
                    author: "Dustin",
                },
            });
            var result = shell("echo 1 > /sys/class/fise_gpio5/level", false);
            toastLog(result);
            break;
        case 1:
            var action = "echo 0 > /sys/class/fise_gpio5/level";
            app.sendBroadcast({
                action: action,
                extras: {
                    author: "Dustin",
                },
            });
            var result = shell("echo 0 > /sys/class/fise_gpio5/level", false);
            toast(result);
            toast("您选择的低电平！");
            break;
    }
});
ui.设置GPIO8.on("click", () => {
    var i = ui.选择电平8.getSelectedItemPosition();
    switch (i) {
        case 0:
            toast("您选择的高电平！");
            var action = "echo 1 > /sys/class/fise_gpio6/level";
            app.sendBroadcast({
                action: action,
                extras: {
                    author: "Dustin",
                },
            });
            var result = shell("echo 1 > /sys/class/fise_gpio6/level", false);
            toast(result);
            break;
        case 1:
            var action = "echo 0 > /sys/class/fise_gpio6/level";
            app.sendBroadcast({
                action: action,
                extras: {
                    author: "Dustin",
                },
            });
            var result = shell("echo 0 > /sys/class/fise_gpio6/level", false);
            toast(result);
            toast("您选择的低电平！");
            break;
    }
});
ui.设置GPIO9.on("click", () => {
    var i = ui.选择电平9.getSelectedItemPosition();
    switch (i) {
        case 0:
            toast("您选择的高电平！");
            var action = "echo 1 > /sys/class/fise_gpio7/level";
            app.sendBroadcast({
                action: action,
                extras: {
                    author: "Dustin",
                },
            });
            var result = shell("echo 1 > /sys/class/fise_gpio7/level", false);
            toast(result);
            break;
        case 1:
            var action = "echo 0 > /sys/class/fise_gpio7/level";
            app.sendBroadcast({
                action: action,
                extras: {
                    author: "Dustin",
                },
            });
            var result = shell("echo 0 > /sys/class/fise_gpio7/level", false);
            toast(result);
            toast("您选择的低电平！");
            break;
    }
});
ui.close.on("click", unregisterReceiver);
events.on("exit", unregisterReceiver);

// setInterval(function () {
//   var action = "android.intent.action.Dustin";
//   app.sendBroadcast({
//     action: action,
//     extras: {
//       author: "Dustin", 
//     },
//   });
//   log("发送了广播", action);
// }, 3000);

// ====================自定义函数=====================================================
function unregisterReceiver() {
    if (flag) {
        receiver && context.unregisterReceiver(receiver);
        flag = false;
        toastLog("关闭广播");
    }
}
